package org.yscz.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yscz.common.HttpStatus;
import org.yscz.common.Result;
import org.yscz.exception.AlgAnalysisException;
import org.yscz.exception.CustomUserException;
import org.yscz.exception.MediaCommandException;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理
 *
 * @author achen
 * @create 2021-08-13 18:48
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {


    /**
     * 统一异常处理@ExceptionHandler,主要用于Exception
     */
    @ExceptionHandler(Exception.class)
    public Result error(HttpServletRequest request, Exception e) {
        log.error("异常信息：", e);
        return Result.error(e.getMessage());
    }

    /**
     * 处理与用户相关的业务异常
     */
    @ExceptionHandler(CustomUserException.class)
    public Result UserExceptionHandler(HttpServletRequest request, CustomUserException e) {
        log.error("用户信息异常：[{}]", e.getMsg());
        return Result.error(e.getCode(), e.getMsg());
    }

    /**
     * 处理与媒体相关的业务异常
     */
    @ExceptionHandler(MediaCommandException.class)
    public Result MediaExceptionHandler(HttpServletRequest request, MediaCommandException e) {
        log.error("媒体业务异常：[{}]", e.getMsg());
        return Result.error(e.getCode(), e.getMsg());
    }

    /**
     * 处理与算法相关的业务异常
     */
    @ExceptionHandler(AlgAnalysisException.class)
    public Result AlgExceptionHandler(HttpServletRequest request, AlgAnalysisException e) {
        log.error("算法业务异常：[{}]", e.getMsg());
        return Result.error(e.getCode(), e.getMsg());
    }

}
